package com.apollographql.apollo.interceptor;

import com.apollographql.apollo.api.Error;
import com.apollographql.apollo.api.Mutation;
import com.apollographql.apollo.api.Operation;
import com.apollographql.apollo.api.Query;
import com.apollographql.apollo.api.Response;
import com.apollographql.apollo.api.internal.ApolloLogger;
import com.apollographql.apollo.api.internal.Function;
import com.apollographql.apollo.api.internal.Optional;
import com.apollographql.apollo.exception.ApolloException;
import com.apollographql.apollo.interceptor.ApolloInterceptor;
import java.util.List;
import java.util.concurrent.Executor;

public class ApolloAutoPersistedOperationInterceptor implements ApolloInterceptor {
    private static final String PROTOCOL_NEGOTIATION_ERROR_NOT_SUPPORTED = "PersistedQueryNotSupported";
    private static final String PROTOCOL_NEGOTIATION_ERROR_QUERY_NOT_FOUND = "PersistedQueryNotFound";
    private volatile boolean disposed;
    private final ApolloLogger logger;
    final boolean useHttpGetMethodForPersistedOperations;

    public ApolloAutoPersistedOperationInterceptor(ApolloLogger logger2, boolean useHttpGetMethodForPersistedOperations2) {
        this.logger = logger2;
        this.useHttpGetMethodForPersistedOperations = useHttpGetMethodForPersistedOperations2;
    }

    @Override // com.apollographql.apollo.interceptor.ApolloInterceptor
    public void interceptAsync(final InterceptorRequest request, final ApolloInterceptorChain chain, final Executor dispatcher, final CallBack callBack) {
        boolean z = false;
        InterceptorRequest.Builder autoPersistQueries = request.toBuilder().sendQueryDocument(false).autoPersistQueries(true);
        if (request.useHttpGetMethodForQueries || this.useHttpGetMethodForPersistedOperations) {
            z = true;
        }
        chain.proceedAsync(autoPersistQueries.useHttpGetMethodForQueries(z).build(), dispatcher, new CallBack() {
            /* class com.apollographql.apollo.interceptor.ApolloAutoPersistedOperationInterceptor.AnonymousClass1 */

            @Override // com.apollographql.apollo.interceptor.ApolloInterceptor.CallBack
            public void onResponse(InterceptorResponse response) {
                if (!ApolloAutoPersistedOperationInterceptor.this.disposed) {
                    Optional<InterceptorRequest> retryRequest = ApolloAutoPersistedOperationInterceptor.this.handleProtocolNegotiation(request, response);
                    if (retryRequest.isPresent()) {
                        chain.proceedAsync(retryRequest.get(), dispatcher, callBack);
                        return;
                    }
                    callBack.onResponse(response);
                    callBack.onCompleted();
                }
            }

            @Override // com.apollographql.apollo.interceptor.ApolloInterceptor.CallBack
            public void onFetch(FetchSourceType sourceType) {
                callBack.onFetch(sourceType);
            }

            @Override // com.apollographql.apollo.interceptor.ApolloInterceptor.CallBack
            public void onFailure(ApolloException e) {
                callBack.onFailure(e);
            }

            @Override // com.apollographql.apollo.interceptor.ApolloInterceptor.CallBack
            public void onCompleted() {
            }
        });
    }

    @Override // com.apollographql.apollo.interceptor.ApolloInterceptor
    public void dispose() {
        this.disposed = true;
    }

    /* access modifiers changed from: package-private */
    public Optional<InterceptorRequest> handleProtocolNegotiation(final InterceptorRequest request, InterceptorResponse response) {
        return response.parsedResponse.flatMap(new Function<Response, Optional<InterceptorRequest>>() {
            /* class com.apollographql.apollo.interceptor.ApolloAutoPersistedOperationInterceptor.AnonymousClass2 */

            public Optional<InterceptorRequest> apply(Response response) {
                if (response.hasErrors()) {
                    if (ApolloAutoPersistedOperationInterceptor.this.isPersistedQueryNotFound(response.getErrors())) {
                        ApolloLogger apolloLogger = ApolloAutoPersistedOperationInterceptor.this.logger;
                        apolloLogger.w("GraphQL server couldn't find Automatic Persisted Query for operation name: " + request.operation.name().name() + " id: " + request.operation.operationId(), new Object[0]);
                        return Optional.of(request.toBuilder().autoPersistQueries(true).sendQueryDocument(true).build());
                    } else if (ApolloAutoPersistedOperationInterceptor.this.isPersistedQueryNotSupported(response.getErrors())) {
                        ApolloAutoPersistedOperationInterceptor.this.logger.e("GraphQL server doesn't support Automatic Persisted Queries", new Object[0]);
                        return Optional.of(request);
                    }
                }
                return Optional.absent();
            }
        });
    }

    /* access modifiers changed from: package-private */
    public boolean isPersistedQueryNotFound(List<Error> errors) {
        for (Error error : errors) {
            if (PROTOCOL_NEGOTIATION_ERROR_QUERY_NOT_FOUND.equalsIgnoreCase(error.getMessage())) {
                return true;
            }
        }
        return false;
    }

    /* access modifiers changed from: package-private */
    public boolean isPersistedQueryNotSupported(List<Error> errors) {
        for (Error error : errors) {
            if (PROTOCOL_NEGOTIATION_ERROR_NOT_SUPPORTED.equalsIgnoreCase(error.getMessage())) {
                return true;
            }
        }
        return false;
    }

    public static class Factory implements ApolloInterceptorFactory {
        final boolean persistMutations;
        final boolean persistQueries;
        final boolean useHttpGet;

        public Factory(boolean useHttpGet2, boolean persistQueries2, boolean persistMutations2) {
            this.useHttpGet = useHttpGet2;
            this.persistQueries = persistQueries2;
            this.persistMutations = persistMutations2;
        }

        public Factory() {
            this(false, true, true);
        }

        @Override // com.apollographql.apollo.interceptor.ApolloInterceptorFactory
        public ApolloInterceptor newInterceptor(ApolloLogger logger, Operation<?, ?, ?> operation) {
            if ((operation instanceof Query) && !this.persistQueries) {
                return null;
            }
            if (!(operation instanceof Mutation) || this.persistMutations) {
                return new ApolloAutoPersistedOperationInterceptor(logger, this.useHttpGet);
            }
            return null;
        }
    }
}
